<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>编辑工程保险</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">
        <div class="block-generic">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>工程名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="enginid" class="layui-input enginid" onclick="openPage('../public/choose_engin.php?dom=enginid&page=kaobao','80%','80%')">
                            <div class="choose_div" id="enginid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_engin.php?dom=enginid&page=kaobao','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">业务归属</label>
                        <div class="layui-input-block">
                            <input type="text" id="engin_gui_unit_name" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">工程负责人</label>
                        <div class="layui-input-block">
                            <input type="text" id="gofuer" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">联系电话</label>
                        <div class="layui-input-block">
                            <input type="text" id="gofutel" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">合同价(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="engin_shimoney" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">工期</label>
                        <div class="layui-input-block">
                            <input type="text" id="engin_gongqi" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>是否投保</label>
                        <div class="layui-input-block">
                            <select id="isbao" name="isbao" lay-filter="isbao" >
                                <option value="">请选择</option>
                                <option value="是">是</option>
                                <option value="否">否</option>
                            </select>
                            <p class="error" id="isbao_error" style="margin: 0;"></p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">工程保险说明</label>
                        <div class="layui-input-block">
                            <textarea id="shuo" name="shuo" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item isbaoshi layui-hide">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">险种</label>
                        <div class="layui-input-block">
                            <div id="xianlei"></div>
                        </div>
                    </div>
                </div>
            </div>
           
            <?php include '../public/upinfo.php'?>
        </div>

        <div class="block-generic isbaoshi layui-hide">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>参保明细</span></div>
                        <div>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_copy_row">复制行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th>保险公司名称</th>
                            <th>投保日期</th>
                            <th>保险生效日期</th>
                            <th>保险终止日期</th>
                            <th>保险费</th>
                            <th>保险费支付方式</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>


    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;


    $("#commentForm").validate({
        rules: {
        },
        messages:{
        }
    });

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_kaobao_edit,data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;

                var engininfo = res.data.engininfo;
                if(engininfo){
                    set_choose_btn('enginid',engininfo.id,engininfo.name);
                    $("#engin_gui_unit_name").val(engininfo.gui_unit_name);
                    $("#gofuer").val(engininfo.gofuer);
                    $("#gofutel").val(engininfo.gofutel);
                    if(engininfo.shiinfo){
                        $('#engin_shimoney').val(engininfo.shiinfo.money);
                    }
                    $("#engin_gongqi").val(engininfo.gongqi);
                }

                xmselect_duo('xianlei', res.data.xianlei, data.xianlei);

                if(data.isbao == '是'){
                    $(".isbaoshi").removeClass('layui-hide');
                }


                $("#isbao").val(data.isbao);
                $("#shuo").val(data.shuo);



                var zhifang = res.data.zhifang;
                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td><input type="text" class="layui-input name"></td>';
                listAddStr += '<td><input type="text" class="layui-input toudt bdate_ymd" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input xiaodt bdate_ymd" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input zhidt bdate_ymd" readonly></td>';
                listAddStr += '<td><input type="number" class="layui-input baofee"></td>';
                listAddStr += '<td><select class="layui-select zhifang">';
                listAddStr += '<option value="">请选择</option>';
                for (var key in zhifang) {
                    listAddStr += '<option value="'+zhifang[key]+'">'+zhifang[key]+'</option>';
                }
                listAddStr += '</select></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr, 1);



                var list = res.data.list;
                if(list.length > 0){
                    var str = '';
                    for (var key in list){
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td><input type="text" class="layui-input name" value="'+list[key].name+'"></td>';
                        str += '<td><input type="text" class="layui-input toudt bdate_ymd" value="'+list[key].toudt+'" readonly></td>';
                        str += '<td><input type="text" class="layui-input xiaodt bdate_ymd" value="'+list[key].xiaodt+'" readonly></td>';
                        str += '<td><input type="text" class="layui-input zhidt bdate_ymd" value="'+list[key].zhidt+'" readonly></td>';
                        str += '<td><input type="number" class="layui-input baofee" value="'+list[key].baofee+'"></td>';
                        str += '<td><select class="layui-select zhifang">';
                        str += '<option value="">请选择</option>';
                        for (var mmm in zhifang) {
                            if(list[key].zhifang == zhifang[mmm]){
                                str += '<option value="'+zhifang[mmm]+'" selected>'+zhifang[mmm]+'</option>';
                            }else{
                                str += '<option value="'+zhifang[mmm]+'">'+zhifang[mmm]+'</option>';
                            }
                        }
                        str += '</select></td>';
                        str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                        str += '</tr>';
                    }
                    $("#list_list").html(str);
                }
                update_refresh_row_func();


                initParseMoney();
                public_upload_file_load('upload_files', res.data.files);
                render_from();
                render_date();
            }
        });

        form.on('select(isbao)', function(data){
            if(data.value == ''){
                $('#isbao_error').show().text('请选择');
                $('#isbao_error').parent().find('input').addClass('select_error');
            }else{
                $('#isbao_error').hide();
                $('#isbao_error').parent().find('input').removeClass('select_error');
            }
            if(data.value == '是'){
                $(".isbaoshi").removeClass('layui-hide');
            }else{
                $(".isbaoshi").addClass('layui-hide');
            }

        });

        form.on('submit(add)', function(data){
            data.field.enginid = get_choose_val('enginid');
            var validRes = $("#commentForm").valid();

            formValid('enginid');
            if(data.field.enginid == undefined || !data.field.enginid){
                validRes = false;
            }


            if(data.field.isbao == ''){
                $('#isbao_error').show().text('请选择');
                $('#isbao_error').parent().find('input').addClass('select_error');
                validRes = false;
            }else{
                $('#isbao_error').hide();
                $('#isbao_error').parent().find('input').removeClass('select_error');
            }


            if(!validRes){return false;}


            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var name = $(this).find('.name').val();
                    var toudt = $(this).find('.toudt').val();
                    var xiaodt = $(this).find('.xiaodt').val();
                    var zhidt = $(this).find('.zhidt').val();
                    var baofee = $(this).find('.baofee').val();
                    var zhifang = $(this).find('.zhifang').val();
                    var explain = $(this).find('.explain').val();

                    list.push({
                        name:name,
                        toudt:toudt,
                        xiaodt:xiaodt,
                        zhidt:zhidt,
                        baofee:baofee,
                        zhifang:zhifang,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;


            data = parseMoney(data);
            data.field.id = param_id;
            data.field.files = public_upload_file_value('upload_files');
            facade.ajax({url: ask_kaobao_edit, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });

            return false;
        });

    });

</script>
</body>
</html>
